﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Data.SqlClient;

namespace CNCSoft.ErpApps.TMS
{
    public partial class frmView : Form
    {
        private string _strSQL;
        private string _strKeyID;
        private string _strDateFieldFilter;

        public string StrSQL
        {
            get { return _strSQL; }
            set { _strSQL = value; }
        }
        private string _SelectedIDs;

        public string SelectedIDs
        {
            get { return _SelectedIDs; }
            set { _SelectedIDs = value; }
        }

        private bool _flChooseAll = false;
        private bool _flChoose = false;

        public bool FlChooseAll
        {
            get { return _flChooseAll; }
            set { _flChooseAll = value; }
        }

        public bool FlChoose
        {
            get { return _flChoose; }
            set { _flChoose = value; }
        }

        private string connectionstring = GlobalVariable.WSInfo.ConnectionString;

        public frmView()
        {
            InitializeComponent();
        }

        public frmView(string strSQL, string strDateFieldFilter)
        {
            InitializeComponent();
            this._strSQL = strSQL;
            this._strDateFieldFilter = strDateFieldFilter;
            
        }

        public frmView(string strSQL, string strKeyID, string strDateFieldFilter)
        {
            InitializeComponent();
            this._strSQL = strSQL;
            this._strKeyID = strKeyID;
            this._strDateFieldFilter = strDateFieldFilter;
        }

        private void frmView_Load(object sender, EventArgs e)
        {
            this.FlChoose = false;
            this.FlChooseAll = false;
            
            dateEditTo.EditValue = DateTime.Now;
            dateEditFrom.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            lblEnquiryDate.Text = _strDateFieldFilter;

            LoadData();
        }

        private void gridControl_DoubleClick(object sender, EventArgs e)
        {
            Choose();
            this.Close();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            Choose();
            this.Close();
        }


        private void txtChooseAll_Click(object sender, EventArgs e)
        {
            ChooseAll();
            this.Close();
        }

        private void btnLoadByEnquiryDate_Click(object sender, EventArgs e)
        {
            try
            {
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region coding

        

        private void LoadData()
        {
            try
            {
                string strSQL = "";
                string strWHERE = "";
                //ko tim thay WHERE
                if (_strSQL.ToUpper().IndexOf("WHERE") == -1 )
                {
                    strWHERE = " WHERE " + _strDateFieldFilter + " BETWEEN '" + Convert.ToDateTime(dateEditFrom.EditValue) + "' AND '" + Convert.ToDateTime(dateEditTo.EditValue) + "' ";
                    strSQL = _strSQL + " " + strWHERE;
                }
                //tim thay WHERE
                else if (_strSQL.ToUpper().IndexOf("WHERE") != -1 )
                {
                    strWHERE = " AND " + _strDateFieldFilter + " BETWEEN '" + Convert.ToDateTime(dateEditFrom.EditValue) + "' AND '" + Convert.ToDateTime(dateEditTo.EditValue) + "' ";
                    strSQL = _strSQL + " " + strWHERE;
                }

                if (_strDateFieldFilter == "")  //Khong co loc ngay chi het, không co where
                {
                    strSQL = _strSQL;
                    dateEditFrom.Visible = false;
                    dateEditTo.Visible = false;
                }

                SqlDataAdapter da = new SqlDataAdapter(strSQL, connectionstring);
                DataSet ds = new DataSet();
                da.Fill(ds);
                gridControl.DataSource = ds.Tables[0];
                lblTotal.Text = String.Format("Total: {0}", ds.Tables[0].Rows.Count.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Choose()
        {
            try
            {
                string s = "";
                int[] selectedrow;
                selectedrow = gridView1.GetSelectedRows();

                for (int i = 0; i < selectedrow.Length; i++)
                {
                    if (i < selectedrow.Length - 1)
                    {
                        s += "'" + getID(selectedrow[i]) + "'" + ",";
                    }
                    else
                    {
                        s += "'" + getID(selectedrow[i]) + "'";
                    }
                }

                this.SelectedIDs = "(" + s + ")";
                this.FlChooseAll = false;
                this.FlChoose = true;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ChooseAll()
        {
            try
            {
                string s = "";
                int selectedall = gridView1.RowCount;

                for (int i = 0; i < selectedall; i++)
                {
                    if (i < selectedall - 1)
                    {
                        s += "'" + getID(i) + "'" + ",";
                    }
                    else
                    {
                        s += "'" + getID(i) + "'";
                    }
                }
                this.SelectedIDs = "(" + s + ")";
                this.FlChooseAll = true;
                this.FlChoose = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string getID(int indexrow)
        {
            //Truong hop nay thi _strKeyID = FileName cua cot dau tien
            if (_strKeyID == "" || _strKeyID == null)   //Chi new 1 tham so
            {
                try
                {
                    _strKeyID = gridView1.Columns[0].FieldName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }


            try
            {
                return gridView1.GetRowCellValue(indexrow, _strKeyID).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "";
            }
        }

        
        #endregion
    }
}
